; (function () {
    $(function () {
        $("#header").load("./header.html")
        $("#footer").load("./footer.html")
        $(".buttons").children().eq(0).on("click",function(){
            window.location = "index.html"
        })
      


        $(".buttons").children("button").on("mouseenter", function () {
            $(this).css("background", "#5643ec")
        })
        $(".buttons").children("button").on("mouseleave", function () {
            $(this).css("background", "#d0d0d0")
        })
        //

        $(document).scroll(function () {
            if ($(document).scrollTop() > 300) {
                $(".backTop").show();
            } else {
                $(".backTop").hide();
            }
        })
    
    
        $(".backTop").on("mouseenter", function () {
            $(this).css({
                "width":"110px",
                "opacity":"1"
            })
            $(".textTop").show().html("返回头部")
        })
        $(".backTop").on("mouseleave", function () {
            $(this).css({
                "width":"41px",
                "opacity":0.5
            })
            $(".textTop").hide();
           
        })
    
        $(".backTop").on("click", function () {
            $("html").animate({ scrollTop: 0 }, 100, function () { })
        })

        
        $.idcode.setCode(); //加载生成验证码方法

        //定义自己的规则
        $.validator.addMethod("checkName", (value, element, param) => {
            var reg = /[\u4e00-\u9fa5]/;
            return reg.test(value);
        })

        $.validator.addMethod("checkPhone", (value, element, param) => {
            var reg = /^[1][3-9]\d{9}$/i;
            return reg.test(value);
        })
        $.validator.addMethod("checkEmail", (value, element, param) => {
            var reg = /^\w+(\.\w+)*@\w+\.\w+(\.\w+)*$/;
            return reg.test(value);
        })
        $.validator.addMethod("checkAddress", (value, element, param) => {
            var reg = /[\u4e00-\u9fa5]/;
            return reg.test(value);
        })


        $("form").validate({
           
            rules: {
                uname: {
                    required: true,
                    checkName: true
                },
                uphone: {
                    checkPhone: true
                },
                uemail: {
                    checkEmail: true
                },
                upwd: {
                    required: true, 
                    rangelength: [6, 18],
                },
                upwd2: {
                    equalTo: "#upwd"
                },
                uaddress: {
                    checkAddress: true
                }

            },
            //提示
            messages: {
                uname: {
                    required: '姓名必填!',
                    checkName: "请输入中文!"
                },
                upwd: {
                    required: '密码必填!',
                    rangelength: "密码必须是{0}-{1}位"
                },
                upwd2: {
                    equalTo: "两次密码不一致!"
                },
                uphone: {
                    checkPhone: "手机号不合法!"
                },
                uemail: {
                    checkEmail: "邮箱不合法!"
                },
                uaddress: {
                    checkAddress: "请填写地址!"
                }
            },
            //提交
            submitHandler: function () {

                var IsBy = $.idcode.validateCode();
                var $check = $(".agreenMe").prop("checked")
                if ($check == false) {
                    $(".agreen .innerText").html("在您接受了www.IKEA.cn的隐私政策，包括个人信息条款后，方可注册!")
                    return false;
                }

                var formdata = new FormData();
                formdata.append("uname",$('[name=uname]').val());
                formdata.append("upwd",$('[name=upwd]').val());
                formdata.append("uphone",$('[name=uphone]').val());
                formdata.append("uemail",$('[name=uemail]').val());
                formdata.append("uaddress",$('[name=uaddress]').val());

                $.ajax({
                    url:"./api/reg",
                    data:formdata,
                    type:"post",
                    processData:false,
                    contentType:false
                }).done(function(res){
                    console.log(res)
                })


                if (!IsBy) {
                    layer.msg('验证码错误!');
                    return false;
                } else {
                    //校验都通过,注册到cookie
                    var obj = {
                        uname: $('[name="uname"]').val(),
                        uphone: $('[name="uphone"]').val(),
                        upwd: $('[name="upwd"]').val(),
                        uemail: $('[name="uemail"]').val(),
                        uaddress: $('[name="uaddress"]').val(),

                    }
                    //1.先读取cookie,转换成数组
                    var userList = JSON.parse($.cookie("users") || '[]');
                    //2.变量数组 判断是否已经注册
                    var flag = userList.some((el) => el.uname == obj.uname)
                    //3.如果没有注册,就保存到数组里
                    if (flag == true) {
                        layer.alert('该用户已注册!');
                        return false;
                    }
                    //4.把数组保存到cookie
                    userList.push(obj);
                    $.cookie.raw = true; 
                    $.cookie("users", JSON.stringify(userList), {
                        expires: 10
                    });

                    //注册成功,是否去登录
                    layer.confirm("注册成功,是否立即去登录?", {
                        skin: 'layui-layer-molv',
                        type: 1,
                        btn: ["好的", "我不要"]
                    }, (index) => {
                        layer.close(index);
                        window.location = "login.html"

                    }, (index) => {
                        layer.close(index); 
                        // window.location.reload();
                       
                    })

                }
                return false;
            }
        })

    })

})()